package com.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.template.entity.Department;
import com.template.entity.User;
import com.template.mapper.UserMapper;
import com.template.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yumuyang
 * @since 2021-11-01
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;

    public Integer isUser(String username) {
        QueryWrapper<User> queryWarpper = new QueryWrapper<>();
        queryWarpper.eq("username", username);
        return userMapper.selectCount(queryWarpper);
    }

    public Integer isPassword(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username)
                .eq("password", password);
        return userMapper.selectCount(queryWrapper);
    }

    public Page<User> selectAllPage(Integer pn) {
        Page<User> userPage = new Page<>(pn,10);
        return userMapper.selectPage(userPage, null);
    }

    public Page<User> selectAllPageByWrapper(Integer pn, Integer departmentid, String keyword) {
        Page<User> userPage = new Page<>(pn, 10);
        Map<String, Object> params = new HashMap<>();
        params.put("departmentid", departmentid);
        params.put("username", keyword);

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.allEq(params, false);
        return userMapper.selectPage(userPage, queryWrapper);
    }

    public void deleteUserById(Integer id) {
        userMapper.deleteById(id);
    }

    public void saveUser(User user) {
        userMapper.insert(user);
    }

    public void updateUser(User user) {
        userMapper.updateById(user);
    }

    public User getUserInfo(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username)
                .eq("password", password);
        return userMapper.selectOne(queryWrapper);
    }
}
